package com.breeze.hostapp

import android.content.Context
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.breeze.mypluginlib.IBean
import dalvik.system.DexClassLoader
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    private val apkName = "app-debug.apk"//apk名称
    override fun attachBaseContext(newBase: Context?) {
        super.attachBaseContext(newBase)
        // 把asset中给定apk复制到 /data/data/files/下
        Utils.extractAssets(newBase, apkName)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val dexPath = getFileStreamPath(apkName).path
        val dexClassLoader = DexClassLoader(dexPath, null, null, classLoader)
        btn_call.setOnClickListener {
            var loadClass = dexClassLoader.loadClass("com.breeze.plugin1.Bean")
            // 1. 反射方式调用插件方法
            /*var beanObj = loadClass.newInstance()
            var method = loadClass.getMethod("getName")
            var name = method.invoke(beanObj) as String
            tv_content.text = name
            */
            // 2. 面向接口编程
            var beanObj = loadClass.newInstance() as IBean
            beanObj.setName("breeze-wang")
            tv_content.text = beanObj.getName()
        }
    }
}